package com.alibaba.user.service.impl;

import com.alibaba.basic.exception.BusinessException;
import com.alibaba.basic.service.impl.BaseServiceImpl;
import com.alibaba.basic.util.Md5Utils;
import com.alibaba.basic.util.StrUtils;
import com.alibaba.user.domain.Logininfo;
import com.alibaba.user.domain.User;
import com.alibaba.user.dto.UserDto;
import com.alibaba.user.mapper.LogininfoMapper;
import com.alibaba.user.mapper.UserMapper;
import com.alibaba.user.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 业务实现类：
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public void phoneRegister(UserDto userDto) {
        String phone = userDto.getPhone();
        String inputPhoneCode = userDto.getPhoneCode();
        String password = userDto.getPassword();
        String passwordRepeat = userDto.getPasswordRepeat();
        // 一 校验是否为空置
        if(StringUtils.isEmpty(phone) ||StringUtils.isEmpty(inputPhoneCode)||
                StringUtils.isEmpty(password)||StringUtils.isEmpty(passwordRepeat) ){
            throw  new BusinessException("信息不能为空");

        }
        // 二 检验密码是否一致
        if (!password.equals(passwordRepeat)){
            throw  new BusinessException("密码输入不一致");
        }
        // 三 验证手机验证码是否过期  是否正确
        Object redisPhoneCode = redisTemplate.opsForValue().get("register:" + phone);
        if (redisPhoneCode == null){
            throw  new BusinessException("手机验证码过期");
        }
        if (!inputPhoneCode.equals(redisPhoneCode.toString().split(":")[0]) ){
            throw  new BusinessException("手机验证码不正确");
        }
        // 四.添加到数据库
        User user = userDto2User(userDto);
        Logininfo logininfo =  user2Logininfo(user);
        // 1.先添加到loginfo中
        logininfoMapper.add(logininfo);
        // 2.在通过loginfoid添加到user中
            user.setLogininfoId(logininfo.getId());
            userMapper.add(user);
    }

    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        // 按属性同名原则进行拷贝
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(1);
        logininfo.setDisable(true);
        return logininfo;
    }

    private User userDto2User(UserDto userDto) {
        User user = new User();
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());
        String salt = StrUtils.getComplexRandomString(32);
        user.setSalt(salt);
        String md5 = Md5Utils.encrypByMd5(userDto.getPassword() + salt);
        user.setPassword(md5);
        user.setState(1);
        return user;
    }

    @Override
    @Transactional
    public void add(User user) {
        //  修改盐值和加密
        String salt = StrUtils.getComplexRandomString(32);
        String md5 = Md5Utils.encrypByMd5(user.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5);

        // 先添加loginfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(1);
        logininfo.setDisable(true);
        logininfoMapper.add(logininfo);

        // 在添加到user
        user.setLogininfoId(logininfo.getId());
        super.add(user);
    }

    @Override
    @Transactional
    public void update(User user) {
        //  修改盐值和加密
        String salt = StrUtils.getComplexRandomString(32);
        String md5 = Md5Utils.encrypByMd5(user.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5);

        User oldUser = userMapper.findById(user.getId());

    // 修改user信息
        user.setLogininfoId(oldUser.getLogininfoId());
        super.update(user);
    // 修改loginfo信息
        Logininfo oldLoginfo = logininfoMapper.findById(oldUser.getLogininfoId());
        BeanUtils.copyProperties(user,oldLoginfo);
        // 将以前的id设置进去
        oldLoginfo.setId(oldUser.getLogininfoId());
        logininfoMapper.update(oldLoginfo);


    }





    @Override
    @Transactional
    public void delete(Long id) {
        User user = userMapper.findById(id);
        super.delete(id);

        // 删除loginfoid
        logininfoMapper.delete(user.getLogininfoId());
    }
}
